package com.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class BaseIOUtils {

    private final static Logger logger = LoggerFactory.getLogger(BaseIOUtils.class);

    /**
     * 将文件读取为String类型
     * @param path 文件地址
     * @param charset 编码
     * @return
     */
    public static String readSmallTextFileToString(String path,String charset) throws Exception{
        File file = new File(path);
        return readSmallTextFileToString(file,charset);
    }

    /**
     * 将文件读取为String类型
     * @param file 文件
     * @param charset 编码
     * @return
     * */
    public static String readSmallTextFileToString(File file, String charset) throws Exception{
        charset=charset==null? Charset.defaultCharset().displayName():charset;
        String str = null;
        InputStream is=null;
        ByteArrayOutputStream bos=null;
        try {
            is = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            int read;
            byte buffer[] = new byte[1024];
            while((read=is.read(buffer))>-1){
                bos.write(buffer,0,read);
            }
            str = new String(bos.toByteArray(),charset);
        } catch (Exception e) {
            throw e;
        } finally {
            try{
                is.close();
            }catch (Exception e){}
            try{
                bos.close();
            }catch (Exception e){}
        }
        return str;
    }

    /**
     * 从输入流中解压文件
     * @param is 文件流
     * @param saveRootPath 文件保存的真实根路径
     * @throws Exception
     */
    public static void unzipFromInputStream(InputStream is,String saveRootPath) throws Exception{
        ZipInputStream zipInputStream = null;
        ZipEntry zipEntry=null;
        FileOutputStream fos=null;
        try {
            zipInputStream = new ZipInputStream(is, Charset.forName("UTF-8"));
            int read;
            byte []buffer = new byte[1024];
            while((zipEntry=zipInputStream.getNextEntry())!=null){
                if(zipEntry.isDirectory()){
                    new File(saveRootPath+"/"+zipEntry.getName()).mkdirs();
                }else{
                    fos = new FileOutputStream(saveRootPath+"/"+zipEntry.getName());
                    while((read=zipInputStream.read(buffer))>-1){
                        fos.write(buffer,0,read);
                    }
                    fos.flush();
                    fos.close();
                }
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try{
                zipInputStream.close();
            }catch (Exception e){
            }
            try{
                fos.close();
            }catch (Exception e){
            }
        }
    }

    public static void copyFromStream(InputStream is,OutputStream os){
        try{
            int read=0;
            byte []buffer = new byte[1024];
            while((read=is.read(buffer))>-1){
                os.write(buffer,0,read);
            }
            os.flush();
        }catch (Exception e){
            logger.error("copyFromStream 拷贝失败！{}",e);
        }finally {
            try {
                os.close();
            }catch (Exception e){}
            try{
                is.close();
            }catch (Exception e){}
        }
    }

}
